package com.netflexitysolutions.amazonws.ec2.internal.operations;

import java.util.List;
import java.util.Set;

import com.amazonaws.ec2.doc._2009_04_04.DescribeSecurityGroupsResponseType;
import com.amazonaws.ec2.doc._2009_04_04.DescribeSecurityGroupsSetItemType;
import com.amazonaws.ec2.doc._2009_04_04.DescribeSecurityGroupsSetType;
import com.amazonaws.ec2.doc._2009_04_04.DescribeSecurityGroupsType;
import com.netflexitysolutions.amazonws.ec2.EC2;
import com.netflexitysolutions.amazonws.ec2.SecurityGroup;
import com.netflexitysolutions.amazonws.ec2.internal.ModelUtil;

public class DescribeSecurityGroupsOperation extends ObjectSetOperationExecutor<DescribeSecurityGroupsType, List<SecurityGroup>> {

	public DescribeSecurityGroupsOperation(EC2 ec2, Set<String> objectIds) {
		super(ec2, objectIds);
	}

	@Override
	protected List<SecurityGroup> call(DescribeSecurityGroupsType request) {
		
		if (objectIds != null) {
			DescribeSecurityGroupsSetType set = new DescribeSecurityGroupsSetType();
			List<DescribeSecurityGroupsSetItemType> list = set.getItem();
			for (String groupName : objectIds) {
				DescribeSecurityGroupsSetItemType item = new DescribeSecurityGroupsSetItemType();
				item.setGroupName(groupName);
				list.add(item);
			}
			request.setSecurityGroupSet(set);
		}
		DescribeSecurityGroupsResponseType response = getEC2().getService().describeSecurityGroups(request);
		return ModelUtil.toDomainType(response.getSecurityGroupInfo());
	}

}
